package com.fhzn.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhzn.auth.constant.Constants;
import com.fhzn.auth.entity.DataResource;
import com.fhzn.auth.entity.UserDataResource;
import com.fhzn.auth.enums.WebResponseEnum;
import com.fhzn.auth.mapper.DataResourceMapper;
import com.fhzn.auth.mapper.UserDataResourceMapper;
import com.fhzn.auth.service.OperatorLogService;
import com.fhzn.auth.service.UserDataResourceService;
import com.fhzn.auth.web.request.GrantUserDataResourceRequest;
import com.fhzn.auth.web.request.UserDataResourceRequest;
import com.fhzn.commons.toolkit.exception.BuzException;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/1/18
 */
@Service
@RequiredArgsConstructor
public class UserDataResourceServiceImpl extends ServiceImpl<UserDataResourceMapper, UserDataResource> implements UserDataResourceService {


    private final DataResourceMapper dataResourceMapper;
    private final UserDataResourceMapper userDataResourceMapper;
    private final OperatorLogService operatorLogService;

    @Override
    public List<String> getUsersByDataResource(UserDataResourceRequest request) {
        request.checkBaseParams();
        List<Integer> dataResourceIds = dataResourceMapper.getUsersByDataResource(request.getCode(), request.getAppCode(), request.getResourceType()).stream().map(DataResource::getId).collect(Collectors.toList());
        return userDataResourceMapper.getUsersByDataResource(dataResourceIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteAndGrantDataResource(GrantUserDataResourceRequest request) {
        DataResource dataResource = dataResourceMapper.selectDataResourceByTypeAndCode(request.getInvokeAppName(), request.getResourceType(), request.getCode());
        if (Objects.isNull(dataResource)) {
            throw new BuzException("资源【" + request.getCode() + "】不存在");
        }
        if (dataResource.getStatus() != 1) {
            throw new BuzException("资源【" + request.getCode() + "】已被禁用，不允许操作");
        }
        userDataResourceMapper.deleteByDataResourceId(dataResource.getId());
        List<UserDataResource> userDataResList = Lists.newArrayList();
        for (String targetUser : request.getTargetUsers()) {
            UserDataResource userDataRes = new UserDataResource();
            userDataRes.setNickname(targetUser);
            userDataRes.setDataResId(dataResource.getId());
            userDataRes.setCreator(request.getHandler());
            userDataRes.setModifier(request.getHandler());
            userDataResList.add(userDataRes);
        }
        boolean inserted = userDataResourceMapper.saveBatch(userDataResList);
        if (!inserted) {
            throw new BuzException("配置用户数据权限异常");
        }
        // 记录操作日志
        for (String targetUser : request.getTargetUsers()) {
            operatorLogService.operatorLog(request.getHandler(), Constants.RESOURCE_SECTION, Constants.DATA_USER_RESOURCE_ADD_METHOD, targetUser, null, request.getCode(), Constants.AUTH_APP_CODE);
        }
        return WebResponseEnum.SUCCESS.getMessage();
    }
}
